//
//  UIViewController+RAExtension.m
//  RayoSmartBleKey
//
//  Created by Piccolo on 2017/12/7.
//  Copyright © 2017年 Piccolo. All rights reserved.
//

#import "UIViewController+RAExtension.h"

@implementation UIViewController (RAExtension)

- (void)ra_setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated {
    if (self.navigationController.navigationBarHidden != hidden) {
        [self.navigationController setNavigationBarHidden:hidden animated:animated];
    }
}

- (void)ra_setTabBarHidden:(BOOL)hidden animated:(BOOL)animated completion:(void (^)(BOOL))completion {
    [self setTabBarVisible:!hidden animated:animated completion:completion];
}

// pass a param to describe the state change, an animated flag and a completion block matching UIView animations completion
- (void)setTabBarVisible:(BOOL)visible animated:(BOOL)animated completion:(void (^)(BOOL))completion {
    
    // bail if the current state matches the desired state
    if ([self tabBarIsVisible] == visible) return (completion)? completion(YES) : nil;
    
    // get a frame calculation ready
    CGRect frame = self.tabBarController.tabBar.frame;
    CGFloat height = frame.size.height;
    CGFloat offsetY = (visible)? -height : height;
    
    // zero duration means no animation
    CGFloat duration = (animated)? 0.3f : 0.0f;
    
    [UIView animateWithDuration:duration animations:^{
        self.tabBarController.tabBar.frame = CGRectOffset(frame, 0, offsetY);
    } completion:completion];
}

//Getter to know the current state
- (BOOL)tabBarIsVisible {
    return self.tabBarController.tabBar.frame.origin.y < CGRectGetMaxY(self.view.frame);
}


@end
